<?php session_start(); ?>
<?php include ("../inc/db_config.inc"); ?>
<?php
//$_SESSION['username']="admin";
$g_title = "所有文章";
$g_tab = "index";
$stab="post";
$g_header = "";
$g_js = '<script type="text/javascript">
$(document).ready(function(){
    //全选 反选 开始
    $("#checkall").change(function(){
        if($(this).attr("checked")==true){
            $("tr").find(":checkbox").attr("checked","checked");
        }else{
            $("tr").find(":checkbox").attr("checked","");
        } 
    })
    //全选反选 结束
    
    //动态删除文章 开始
    $("#delBtn").click(function(){
        var t=$(this);
        var len=$("tr input:checked").length;
        if(len<1){
            alert("请先选中至少一项!");
            return false;
        }
       if(confirm("确定删除吗?")){
        var arr=new Array();
        t.next().show();
        $("tr input:checked").each(function(i,item){
            arr.push($("tr input:checked").eq(i).val());
        })
        $.ajax({
            type:"POST",
            url:"post_del.php",
            "data":{pid:arr,format:"json"},
            success:function(json){
								t.next().hide();
                var obj=eval("("+json+")");
                var status=obj.Status;
                //alert(obj.ReturnCode);
                if(status==1){
                    $("tr input:checked").each(function(i,item){
                            $(this).closest("tr").fadeOut("slow");
                        })
                    //window.location.reload();
                }else{
                    alert("删除失败");
                }
            },
            error:function(){
                alert("error");
            }
        })
        }
    })
    //动态删除文章 结束
    
    //移动文章 开始
    $("#cataList").change(function(){
        var len=$("tr input:checked").length;
        if(len<1){
            alert("请先选择准备移动的文章");
            $(this)[0].selectedIndex=0;
            return false;
        }
        var cataID=$(this).val();
        var cataName=$(this)[0].options[$(this)[0].selectedIndex].text;
        if(cataID==""){
            return false;
        }
       if(confirm("确定移动分类到"+cataName+"吗?")){
        var arr=new Array();
        $("tr input:checked").each(function(i,item){
            arr.push($("tr input:checked").eq(i).val());
        })
        $.ajax({
            type:"POST",
            url:"post_move.php",
            "data":{pid:arr,cid:cataID,format:"json"},
            success:function(json){
                var obj=eval("("+json+")");
                var status=obj.Status;
                //alert(obj.ReturnCode);
                if(status==1){
                    $("tr input:checked").each(function(i,item){
                            $(this).closest("tr").find("td").eq(2).find("a").html(cataName);
                            $(this).attr("checked","");
                            $("#cataList")[0].selectedIndex=0;
                        })
                    //window.location.reload();
                }else{
                    alert("移动失败");
                }
            },
            error:function(){
                alert("error");
            }
        })
        }
    })
    //移动文章 结束
    
    $(".admin_table tbody tr").click(function(){
        $(this).toggleClass("h");
       var ck=$(this).find("input").attr("checked");
       $(this).find("input").attr("checked",(ck?"":"checked"));
    })
    
})
</script>';
?>
 <?php include ('header.php'); ?>
 <?php
function getPageInfo($sql, $ps, $currPage)
{
    global $beginNum, $begin, $end, $page, $totalpage;
    //分页开始
    $result = mysql_query($sql);
    //$all=mysql_fetch_array($rn);
    //$total=$all[0];//全部记录数目
    $total = mysql_num_rows($result);
    $pagesize = $ps;
    $page = $currPage;
    $totalpage = ceil($total / $pagesize);
    if ($page < 1) {
        $page = 1;
    }
    if ($page > $totalpage) {
        $page = $totalpage;
    }
    $begin = $page - 4;
    $end = $page + 5;
    if ($page < 5) {
        $begin = 1;
        $end = 10;
    }
    if ($page > $totalpage - 5) {
        $begin = $totalpage - 9;
        $end = $totalpage;
    }
    if ($begin < 1) {
        $begin = 1;
    }
    if ($end > $totalpage) {
        $end = $totalpage;
    }
    $beginNum = ($page - 1) * $pagesize;


    //分页结束
}
if (isset($_REQUEST['p']) && $_REQUEST['p'] != "") {
    $currPage = $_REQUEST['p'];
} else {
    $currPage = 1;
}
$pagesize = 20;
if(isset($_REQUEST['cid'])&&$_REQUEST['cid']!=""){
    $cid=$_REQUEST['cid'];
}
if($cid){
    getPageInfo("select * from info where type= $cid", $pagesize, $currPage);
}else{
   getPageInfo("select * from info", $pagesize, $currPage); 
}
?>

			<div class="crumb"></div>
			<div class="main">
				<div class="container r">
					<div class="conbox">
						<div class="cb_hd">
							<span class="cb_tl"></span>
							<span class="cb_tr"></span>
							<h2>文章列表</h2>
						</div>
						<div class="cb_bd">
								<div class="article_act">
									<span class="ad_chk"><input id="checkall"  type="checkbox"/> 选中项 <button id="delBtn">删除</button><img class="a_loading" style="display:none;margin-left:10px;" src="i/loading.gif"/></span>
									<span class="ad_show">移动到:<select id="cataList"><option value="">请选择</option>
                                    <?php
$result_type = mysql_query("select * from type");
if (mysql_affected_rows() > 0) {
    while ($row_type = mysql_fetch_array($result_type)) {
?>
                                            <option value="<?php echo $row_type['id']; ?>"><?php echo
$row_type['name']; ?></option>
                                            <?php }
                                            
} ?>
                                    </select></span>
									<?php if ($totalpage > 1) { ?><div class="pageinfo">
										<a href="?p=<?php echo $page - 1; ?>">上一页</a>
										<span><?php echo $page; ?></span>
										<a href="?p=<?php echo $page + 1; ?>">下一页</a>
									</div><?php } ?>
								</div>
								<table class="admin_table">
									<thead>
										<tr><th><th>标题</th><th>分类</th><th>日期</th><th width="60">操作</th></tr>
									</thead>
									<tbody>
                                    <?php
if($cid){                                    
$result_list = mysql_query("select i.id,i.title,i.content,i.created,i.pubid,t.id as tid,t.name from info as i,type as t where i.type=t.id and i.type=$cid order by i.id desc limit $beginNum,$pagesize");
}else{
  $result_list = mysql_query("select i.id,i.title,i.content,i.created,i.pubid,t.id as tid,t.name from info as i,type as t where i.type=t.id  order by i.id desc limit $beginNum,$pagesize");  
}
if (mysql_affected_rows() > 0) {
    while ($row_list = mysql_fetch_array($result_list)) {
?>
										<tr>
											<td><input name="pid" value="<?php echo $row_list['id']; ?>" type="checkbox"/></td>
											<td><a href="../show.php?pid=<?php echo $row_list['id']; ?>" target="_blank"><?php echo
$row_list['title']; ?></a></td>
											<td><a href="post.php?cid=<?php echo $row_list['tid']; ?>"><?php echo $row_list['name']; ?></a></td>
											<td><?php echo $row_list['created']; ?></td>
											<td><a href="post_edit.php?pid=<?php echo
$row_list['id']; ?>">编辑</a><a href="post_del.php?pid[]=<?php echo
$row_list['id']; ?>">删除</a></td>
										</tr>
                                        <?php }
} else { ?>
                                        <tr><td colspan="5">暂无文章</td></tr>
                                        <?php } ?>
									</tbody>
								</table>
							
							
						</div>
						<div class="cb_ft"></div>
					</div>
                    <?php if ($totalpage > 1) { ?>
					<div class="pageinfo">
                    <a href="?p=1">首页</a>
								<a href="?p=<?php echo $page - 1; ?>">上一页</a>
                                <?php
    for ($i = $begin; $i <= $end; $i++) {
        if ($i == $page) {
            echo "&nbsp;<span>" . $i . "</span>";
        } else {
            echo "&nbsp;<a href='?p=$i'>" . $i . "</a>";
        }
    }
?>
								<a href="?p=<?php echo $page + 1; ?>">下一页</a>
                                <a href="?p=<?php echo $totalpage; ?>">尾页</a>
							</div><?php } ?>
				</div>
				<?php include ('sidebar.php'); ?>
				<div class="c"></div>
			</div>
		</div>
	</div>
 <?php include ('footer.php'); ?>
